<?php
include 'PHPClasses/RelationshipHandler.php';

  
class StudentDBAccess extends DBAccess {
    //put your code here
     private $idStudentGenerated;
     private $relationshipHandler;
    
    //StudentDBAccess Constructor
    public function _construct(){
        //connect
        parent::__construct();
    }   
    
    public function validate($Student,$registered)
    {
        $errormsg = "";
       
        $name=$Student->getStudentName();
        $address=$Student->getAddress();
        $age=$Student->getAge();
        $contacts=$Student->getContactNum();
        
        //Check empty fields
        if($name&&$address&&$age&&!empty($contacts)){}
        else  
            $errormsg = "Please fill up all text fields";
                                            
       
            $studentList=mysql_query("SELECT * FROM student WHERE studentName='$name'") or 
             die('Could not connect3: ' . mysql_error());
       
       
       // Check if student is already in database
       if(!$registered){                                     
        if(mysql_num_rows($studentList) > 0)
        {
            if ($errormsg) //If there is already an error, add next error  
                $errormsg = $errormsg . " & student is already registered";  
            else  
                $errormsg = "Student is already registered"; 
        }
       }
        return $errormsg;
    }
    
  
    
    public function addToDB($Student){
        
        //Insert student to database
        $insertStudentQuery=mysql_query("INSERT INTO student (studentName, studentlvl, address, age, gender,passed)
                                                                        VALUES ('".$Student->getStudentName()."','".$Student->getStudentLevel()."','".$Student->getAddress()."',".$Student->getAge().",'".$Student->getGender()."',0)");
        //Get id generated after inserting
        $this->idStudentGenerated= mysql_insert_id();
       
        //Adds relationship of student to his/her subject in database
        $this->relationshipHandler=new RelationshipHandler();
        
        $relateSubjectToStudentsQuery=$this->relationshipHandler->SubjectToStudent($this->idStudentGenerated);
        
     
       //Check if query is successful
       if($insertStudentQuery&&$relateSubjectToStudentsQuery){
        
       }
        
       else{
            die('Could not connect:' . mysql_error());
        }
       
        return $insertStudentQuery;
    }
    
    
    
     public function addContactsToDB($Student){
         
        //Insert contact number/s of student in database
        for($counter=0;$counter<  sizeof($Student->getContactNum());$counter++){
              
              $int=$Student->getContactNum();
              
              if($int[$counter]==0){
                  break;
              }
              
              $insertContactQuery=mysql_query("INSERT INTO Contact_Number (Student_idStudent, number)
                                VALUES (".$this->idStudentGenerated.", '".$int[$counter]."')");
           
        }
       
        //Check if query is successful      
        if($insertContactQuery){}
        else{
            die('Connect error:'.mysql_error());
        }
     
       
        
    }
    
    public function updateDB($Student) {
        
        //Update the student 
        $updateStudentQuery=mysql_query("UPDATE student SET studentname='".$Student->getStudentName()."' , studentlvl='".$Student->getStudentLevel()."', address='".$Student->getAddress()."', age=".$Student->getAge().", gender ='".$Student->getGender()."' WHERE idStudent =".$Student->getIdStudent()) 
                or die('Could not connect: ' . mysql_error());
        
        //Update the level of grades in database
        $updateUnarchivedGrades=mysql_query("UPDATE grade SET level='".$Student->getStudentLevel()."' WHERE student_idStudent =".$Student->getIdStudent()." AND archived=0") 
                 or die('Could not connect: ' . mysql_error());
        
        //Update relationship of student with his/her subjects
        $this->relationshipHandler=new RelationshipHandler();
            
        $deleteExistingSubjectToStudentQuery=mysql_query("DELETE FROM student_has_subject WHERE idStudent=".$Student->getIdStudent());
       
        $relateSubjectToStudentsQuery=$this->relationshipHandler->SubjectToStudent($Student->getIdStudent());
        
        
        //Update grades
        $updateGradesQuery=$this->relationshipHandler->updateGrades($Student->getIdStudent());
      
        
        //Check if queries are successful   
        if($updateStudentQuery&&$deleteExistingSubjectToStudentQuery&&$relateSubjectToStudentsQuery&&$updateUnarchivedGrades&&$updateGradesQuery){
          
         }
        
        else{
          die('Connect error:'.mysql_error());
        }
      
        
                                            
    }
    
    
    public function updateContactsDB($Student){
            
        //Update Contacts of Student
            $idContact = mysql_query("Select idContact_Number from Contact_Number WHERE Student_idStudent =".$Student->getIdStudent()."");
            $int=$Student->getContactNum();
            
            for($counter=0;$counter<sizeof($Student->getContactNum())&&$row = mysql_fetch_array($idContact);$counter++){
                extract($row);
            
              if($int[$counter]==0){
                  break;
              }
              
                $updateContactQuery= mysql_query("UPDATE Contact_Number Set number =$int[$counter] WHERE idContact_Number = $idContact_Number");
             
            }
            
            //Check if query is successful 
            if($updateContactQuery){}
            else die('Connect error:'.mysql_error());
      
        
    }
   
     public function searchDB($string) {
         //Select student query with the name 'string'   
         $searchStudentQuery=mysql_query("SELECT * FROM student WHERE studentName LIKE '%$string%'");
          
         return $searchStudentQuery;
     }
     
     
      public function getCountInDB($string) {
          //Number of students returned by searchDB
           $countResultsQuery=mysql_query("SELECT count(*) AS 'num' FROM (SELECT * FROM student WHERE studentName LIKE '%$string%') t");
      
           return $countResultsQuery;
      }  
        
        
     public function viewDB(){
        //Select all students in database
       $studentList=mysql_query("SELECT * FROM student ORDER BY studentlvl,passed") or 
                                die('Could not connect: ' . mysql_error());
   
       return $studentList;
    
     }
        
     public function viewProfile($idStudent){
            //Select the student with the idStudent
                $studentProfile=mysql_query("SELECT * FROM student WHERE idStudent=".$idStudent."") or die('Could not connect: ' . mysql_error());
                
                $row = mysql_fetch_array($studentProfile);
    
                return $row;
    }
    
    public function viewStudentContacts($idStudent){
           // Select the contact numbers of the student returned by viewProfile
          $contact=mysql_query("SELECT number FROM Contact_Number WHERE Student_idStudent=".$idStudent."") or die('Could not connect: ' . mysql_error());
    
          return $contact;
    }

        
    public function viewStudentGrades($idStudent,$studentlvl,$archived){
           
            //View the student's grades on their subjects
                        $studentSubjectsID=mysql_query("SELECT * FROM student_has_subject WHERE idStudent='$idStudent'") or 
                                                                die('Could not connect: ' . mysql_error());
                        if($archived==1){
                        $studentSubjectsID=mysql_query("SELECT gradecriterion_subject_idSubject, student_idStudent FROM grade 
                             WHERE student_idStudent='$idStudent' GROUP bY gradecriterion_subject_idSubject") or die('Could not connect: ' . mysql_error()) ;
                        }
                                 echo "<table id=hor-minimalist-a>";
                                  
                                    echo "<thead><tr><th>Subject Name</th>";
                                    echo   "<th scope=col>Passing Grade</th>
                                            <th scope=col>Q1</th>
                                            <th scope=col>Q2</th>
                                            <th scope=col>Q3</th>
                                            <th scope=col>Q4</th>
                                            <th scope=col>Total</th></tr></thead>";
                                  
                     if(mysql_num_rows($studentSubjectsID)!=0){
                                  $gc=new GradeComputation();  
                                  while($studentSubjectsIDrow=mysql_fetch_array($studentSubjectsID))
                                    {
                                       extract($studentSubjectsIDrow);
                                       
                                      
                                        if($archived==1){
                                            $studentSubjects=mysql_query("SELECT * FROM subject, subject_yearlevel WHERE idSubject='$gradecriterion_subject_idSubject' AND subject_yearlevel.Level = '".$studentlvl."' AND subject_idSubject = '$gradecriterion_subject_idSubject'") or 
                                                                die('Could not connect: ' . mysql_error());
                                        }
                                        
                                        else{
                                              $studentSubjects=mysql_query("SELECT * FROM subject, subject_yearlevel WHERE idSubject='$idSubject'AND subject_yearlevel.Level = '".$studentlvl."' AND subject_idSubject = '$idSubject'") or 
                                                                die('Could not connect: ' . mysql_error());
                                        }
                                        
                                        while($studentSubjectsRow = mysql_fetch_array($studentSubjects))
                                        {
                                            extract($studentSubjectsRow);
                                           if($archived==0){
                                            echo "<tr>
                                           <td><a href='inputStudentGrade.php?subjectID=".$idSubject."&studentID=".$idStudent."' style='text-decoration:none; color:#636363;'>$subjectName</a></td>\n";
                                           }
                                           
                                           else{
                                                echo "<tr>
                                                <td><a style='text-decoration:none; color:#636363;'>$subjectName</a></td>\n";
                                           }
                                           
                                           //Grade averages on a particular subject
                                             $first=$gc->computeSubjectAverage($idStudent, $idSubject,"1st Quarter",$studentlvl,$archived);
                                             $second=$gc->computeSubjectAverage($idStudent, $idSubject,"2nd Quarter",$studentlvl,$archived);
                                             $third=$gc->computeSubjectAverage($idStudent, $idSubject,"3rd Quarter",$studentlvl,$archived);
                                             $fourth=$gc->computeSubjectAverage($idStudent, $idSubject,"4th Quarter",$studentlvl,$archived);
                                             $totalSubject=$gc->computeSubjectTotalAverage($first,$second,$third,$fourth);
                                             
                                               
                                            echo "<td>$passingGrade</td>";
                                            echo "<td>$first</td>";
                                            echo "<td>$second</td>";
                                            echo "<td>$third</td>";
                                            echo "<td>$fourth</td>";
                                            
                                           
                                            //Check if the student passed the subject
                                            if($totalSubject>=$passingGrade){
                                              
                                                if($totalSubject>=90)
                                                    echo "<td style='color:blue'>$totalSubject</td>";
                                                
                                                else
                                                    echo "<td style='color:green'>$totalSubject</td>";
                                            }
                                            
                                            else{
                                                echo "<td style='color:red'>$totalSubject</td>";
                                                
                                                $passed=FALSE;
                                            }

                                        }
                                        
                                    }
                                    
                                    //Grade averages per quarter
                                    $first=$gc->computeQuarterAverage($idStudent,'1st Quarter',$studentlvl,$archived);
                                    $second=$gc->computeQuarterAverage($idStudent,'2nd Quarter',$studentlvl,$archived);
                                    $third=$gc->computeQuarterAverage($idStudent,'3rd Quarter',$studentlvl,$archived);
                                    $fourth=$gc->computeQuarterAverage($idStudent,'4th Quarter',$studentlvl,$archived); 
                                    $total=$gc->computeTotalAverage($idStudent,$studentlvl,$archived);
                                    
                                    
                                    echo "<tr><td></td><td></td><th></th><th></th><th></th><th></th><th></th></tr><tr><td></td><td></td><td>$first</td><td>$second</td><td> $third</td><td> $fourth</td><td>$total</td>";    
                                    echo "</tr></table><br/>\n";
                                    
                     }
                     else{               
                                    echo "<tr><td>Student has no subject</td>";    
                                    echo "</tr></table><br/>\n";
                     }         
                     
                     if(!isset($passed)&&$archived==0){
                         $passed=TRUE;
                     }
                     
                     //Mark student as passed
                     if(isset($passed)&&$passed){
                         mysql_query("UPDATE student set passed=1 WHERE idStudent=".$idStudent);
                     }
                     
                     //Mark student as failed
                     else{
                          mysql_query("UPDATE student set passed=0 WHERE idStudent=".$idStudent);
                     }
                               
                           
            
        }
        
        
       
       
        public  function delete($idStudent) {
            
           //delete grades of student 
           $deleteStudentGradesQuery=mysql_query("DELETE FROM Grade WHERE student_idStudent ='".$idStudent."'") ;
   
           //delete relationship of student to his or her subjects
           $deleteStudentToSubjectQuery=mysql_query("DELETE FROM Student_has_Subject WHERE idStudent ='".$idStudent."'") ;
    
           //delete contact number/s of students
           $deleteContactNum=mysql_query("DELETE FROM Contact_Number WHERE Student_idStudent ='".$idStudent."'") ;
    
           //delete the student
           $deleteStudent=mysql_query("DELETE FROM student WHERE idStudent ='".$idStudent."'") ;
           
           //check if queries are successful
           if($deleteStudentGradesQuery&&$deleteStudentToSubjectQuery&&$deleteContactNum&&$deleteStudent){
               
           }
           
           else{
                die('Could not connect: ' . mysql_error());
           }
           
        }
        
        
        public function listgrade($subjectID, $studID){
            
            //lists all of the exams,quizzes,etc. of the student from 1st quarter to 4th Quarter; inputting/updating of grades
              $p=1;
                                        $studentlevel=mysql_query("SELECT studentlvl FROM student WHERE idStudent=".$studID);
                                        
                                        $studentRow=mysql_fetch_array($studentlevel);
                                        
                                        extract($studentRow);
                                        
                                        $level=$studentlvl;
              
                                        $gradeCriteriaList=mysql_query("SELECT name,percentage,idGradeCriteria FROM gradecriterion, subject  WHERE subject.idSubject= ".$subjectID." AND gradecriterion.subject_idSubject=subject.idSubject
                                                   AND gradecriterion.quarter = '1st Quarter'") or 
                                                                        die('Could not connect: ' . mysql_error());
                                            
                                                    echo "<p>1st Quarter</p>";                        
                                           echo   "<form name='studentIDForm' action='inputStudentGradeDB.php' method='POST'>";
                                                    
                                                while($row = mysql_fetch_array($gradeCriteriaList)){
                                                    extract($row);
                                                     $q=1;
                                                     $gradetable=mysql_query("SELECT idGrade , grade FROM grade WHERE gradecriterion_subject_idSubject=".$subjectID." AND student_idStudent=".$studID." AND gradecriterion_idGradeCriteria= ".$idGradeCriteria." AND archived=0 AND level='".$level."'") or 
                                                                        die('Could not connect: ' . mysql_error());
                                                    
                                                     echo" <p>$name: $percentage";
                                                while($row1 = mysql_fetch_array($gradetable)){
                                                    
                                                    extract($row1);
                                                    echo"<p>".$name." ".$q.":</p><input style='padding:0;' class='tf_effect' onkeypress='return isNumberKey(event)' maxlength='3' value='$grade' type='text' name='oldgrades[$idGradeCriteria][]'/>";
                                                    echo" <input type='hidden' value='$idGrade' name='idgrade[]'/>";
                                                    $q++;
                                                }
                                                    echo"<div id='grade".$p."'></div></p>";
                                                    
                                                    echo"  <input type='hidden' value='$idGradeCriteria' name='gradeCriteria[]'/>";
                                                      echo"  <input type='hidden' value='$studID' name='studentID'/>";
                                                      
                                                     echo"  <input type='button' onmousedown='addInput1stQuarter(".$idGradeCriteria.",".$q.",".$p.",\"".$name."\");' class='addmore' value='Add More Grade'/>";
                                                      echo"  <input type='button' onmousedown='deleteinput(".$p.");' class='addmore' value='Delete'/>";
                                                     
                                                     $p++;
                                                     
                                                }
                                                   $gradeCriteriaList=mysql_query("SELECT name,percentage, idGradeCriteria FROM gradecriterion, subject  WHERE subject.idSubject= ".$subjectID." AND gradecriterion.subject_idSubject=subject.idSubject
                                                   AND gradecriterion.quarter = '2nd Quarter'") or 
                                                                        die('Could not connect: ' . mysql_error());
                                                    echo "<p>2nd Quarter</p>";                        
                                           echo   "<form name='studentIDForm' action='inputStudentGradeDB.php' method='POST'>";
                                           
                                                while($row = mysql_fetch_array($gradeCriteriaList)){
                                                   extract($row);
                                                     $q=1;
                                                     $gradetable=mysql_query("SELECT idGrade , grade FROM grade WHERE gradecriterion_subject_idSubject=".$subjectID." AND student_idStudent=".$studID." AND gradecriterion_idGradeCriteria= ".$idGradeCriteria." AND archived=0 AND level='".$level."'") or 
                                                                        die('Could not connect: ' . mysql_error());
                                                     echo" <p>$name: $percentage";
                                                while($row1 = mysql_fetch_array($gradetable)){
                                                    
                                                    extract($row1);
                                                    echo"<p>".$name." ".$q.":</p><input style='padding:0;' class='tf_effect' value='$grade' onkeypress='return isNumberKey(event)' maxlength='3' type='text' name='oldgrades[$idGradeCriteria][]'/>";
                                                    echo" <input type='hidden' value='$idGrade' name='idgrade[]'/>";
                                                    $q++;
                                                }
                                                    echo"<div id='grade".$p."'></div></p>";
                                                    
                                                    echo"  <input type='hidden' value='$idGradeCriteria' name='gradeCriteria[]'/>";
                                                      echo"  <input type='hidden' value='$studID' name='studentID'/>";
                                                      
                                                   echo"  <input type='button' onmousedown='addInput1stQuarter(".$idGradeCriteria.",".$q.",".$p.",\"".$name."\");' class='addmore' value='Add More Grade'/>";
                                                    echo"  <input type='button' onmousedown='deleteinput(".$p.");' class='addmore' value='Delete'/>";
                                                   $p++;
                                                }
                                                   $gradeCriteriaList=mysql_query("SELECT name, percentage, idGradeCriteria FROM gradecriterion, subject  WHERE subject.idSubject= ".$subjectID." AND gradecriterion.subject_idSubject=subject.idSubject
                                                   AND gradecriterion.quarter = '3rd Quarter'") or 
                                                                        die('Could not connect: ' . mysql_error());
                                                    echo "<p>3rd Quarter</p>";                        
                                           echo   "<form name='studentIDForm' action='inputStudentGradeDB.php' method='POST'>";
                                           
                                                while($row = mysql_fetch_array($gradeCriteriaList)){
                                                     extract($row);
                                                     $q=1;
                                                     $gradetable=mysql_query("SELECT idGrade , grade FROM grade WHERE gradecriterion_subject_idSubject=".$subjectID." AND student_idStudent=".$studID." AND gradecriterion_idGradeCriteria= ".$idGradeCriteria." AND archived=0 AND level='".$level."'") or 
                                                                        die('Could not connect: ' . mysql_error());
                                                     echo" <p>$name: $percentage";
                                                while($row1 = mysql_fetch_array($gradetable)){
                                                    
                                                    extract($row1);
                                                    echo"<p>".$name." ".$q.":</p><input style='padding:0;' class='tf_effect' value='$grade' onkeypress='return isNumberKey(event)' maxlength='3' type='text' name='oldgrades[$idGradeCriteria][]'/>";
                                                    echo" <input type='hidden' value='$idGrade' name='idgrade[]'/>";
                                                    $q++;
                                                }
                                                    echo"<div id='grade".$p."'></div></p>";
                                                    
                                                    echo"  <input type='hidden' value='$idGradeCriteria' name='gradeCriteria[]'/>";
                                                      echo"  <input type='hidden' value='$studID' name='studentID'/>";
                                                      
                                                   echo"  <input type='button' onmousedown='addInput1stQuarter(".$idGradeCriteria.",".$q.",".$p.",\"".$name."\");' class='addmore' value='Add More Grade'/>";
                                                     echo"  <input type='button' onmousedown='deleteinput(".$p.");' class='addmore' value='Delete'/>";
                                                   $p++;
                                                }
                                                   $gradeCriteriaList=mysql_query("SELECT name,percentage, idGradeCriteria FROM gradecriterion, subject  WHERE subject.idSubject= ".$subjectID." AND gradecriterion.subject_idSubject=subject.idSubject
                                                   AND gradecriterion.quarter = '4th Quarter'") or 
                                                                        die('Could not connect: ' . mysql_error());
                                                    echo "<p>4th Quarter</p>";                        
                                           echo   "<form name='studentIDForm' action='inputStudentGradeDB.php' method='POST'>";
                                           
                                                while($row = mysql_fetch_array($gradeCriteriaList)){
                                                    extract($row);
                                                     $q=1;
                                                     $gradetable=mysql_query("SELECT idGrade , grade FROM grade WHERE gradecriterion_subject_idSubject=".$subjectID." AND student_idStudent=".$studID." AND gradecriterion_idGradeCriteria= ".$idGradeCriteria." AND archived=0 AND level='".$level."'") or 
                                                                        die('Could not connect: ' . mysql_error());
                                                     echo" <p>$name: $percentage";
                                                while($row1 = mysql_fetch_array($gradetable)){
                                                    
                                                    extract($row1);
                                                    echo"<p>".$name." ".$q.":</p><input style='padding:0;' class='tf_effect' value='$grade' onkeypress='return isNumberKey(event)' maxlength='3' type='text' name='oldgrades[$idGradeCriteria][]'/>";
                                                    echo" <input type='hidden' value='$idGrade' name='idgrade[]'/>";
                                                    $q++;
                                                }
                                                    echo"<div id='grade".$p."'></div></p>";
                                                    
                                                    echo"  <input type='hidden' value='$idGradeCriteria' name='gradeCriteria[]'/>";
                                                      echo"  <input type='hidden' value='$studID' name='studentID'/>";
                                                      
                                                   echo"  <input type='button' onmousedown='addInput1stQuarter(".$idGradeCriteria.",".$q.",".$p.",\"".$name."\");' class='addmore' value='Add More Grade'/>";
                                                    echo"  <input type='button' onmousedown='deleteinput(".$p.");' class='addmore' value='Delete'/>";
                                                   $p++;
                                                }
                                              
                                                  
                                                echo"  <input type='hidden' value='$subjectID' name='subjectID'/>";
                                                
                                                
                                                   
                                                 echo "<br/><br/><center><input type='submit' id='inputG' class='addmore' value='Save' name='Enter'/></center>" ; 
                                             echo "</form>";   
        }
        public function updategrade($oldgrades,$gradeid,$gradeCriteria){
             $p=0;
                                            for($i=0;$i<sizeof($gradeCriteria);$i++){
                                               if($oldgrades==""){
                                                   break;
                                               }
                                              for($j=0;$j<sizeof($oldgrades[$gradeCriteria[$i]]);$j++){
                                                   if($oldgrades[$gradeCriteria[$i]][$j]==""){
                                                       break;
                                                   }
                                                
                                                   $trial= mysql_query("UPDATE grade SET grade=".$oldgrades[$gradeCriteria[$i]][$j]." WHERE idGrade=".$gradeid[$p]." AND archived=0")
                                                   or die('Could not connect: ' . mysql_error());
                                                 
                                                   $p++;
                                               }
                                            }
        }
        public function addgrade($newgrades,$gradeCriteria,$subjectID,$studentID){
            
            $getLevelQuery= mysql_query("SELECT studentlvl FROM student WHERE idStudent=".$studentID);
            $row = mysql_fetch_array($getLevelQuery);
            extract($row);
            
             for($i=0;$i<sizeof($gradeCriteria);$i++){ 
                                               for($j=0;$j<sizeof($newgrades[$gradeCriteria[$i]]);$j++){
                                                   if($newgrades[$gradeCriteria[$i]][$j]==""){
                                                       break;
                                                   }
                                             
                                               $trial= mysql_query("INSERT INTO grade(student_idStudent,grade,gradecriterion_idGradeCriteria,gradecriterion_subject_idSubject,level,archived)
                                                   VALUES (".$studentID.",".$newgrades[$gradeCriteria[$i]][$j].",".$gradeCriteria[$i].",".$subjectID.",'$studentlvl',0)" )or die('Could not connect: ' . mysql_error());
                                                
                                               
                                                 }
                                                 
                                            }
                                   
        }
        public function validationofgrade($newgrades,$gradeCriteria,$oldgrades){
              $errormsg="";
                                          for($i=0;$i<sizeof($gradeCriteria);$i++){
                                               if($oldgrades==""){
                                                   break;
                                               }
                                               
                                               //check for empty fields
                                              for($j=0;$j<sizeof($oldgrades[$gradeCriteria[$i]]);$j++){
                                                   if($oldgrades[$gradeCriteria[$i]][$j]==""){
                                                       $errormsg = "A previous grade has not been fill up yet";
                                                   }
                                               //check if grade is greater than 100 
                                              if($oldgrades[$gradeCriteria[$i]][$j]>100){
                                                  $errormsg = "A grade/s is greater than 100";
                                              }
                                              
                                                   $p++;
                                               }
                                            }
                                            for($i=0;$i<sizeof($gradeCriteria);$i++){ 
                                               for($j=0;$j<sizeof($newgrades[$gradeCriteria[$i]]);$j++){
                                                   if($newgrades[$gradeCriteria[$i]][$j]==""){
                                                        $errormsg = "A new grade has not been fill up yet";
                                                   }
                                            if($newgrades[$gradeCriteria[$i]][$j]>100){
                                                  $errormsg = "A grade/s is greater than 100";
                                              }
                                                 }
                                                 
                                            }
                                            return $errormsg;
        }
        
        
        public function archiveGrades(){
            
           $arrayLevels = array("Grade 1", "Grade 2", "Grade 3", "Grade 4","Grade 5", "Grade 6","1st Year", "2nd Year","3rd Year", "4th Year");
            
           //array of failed students
           $failed=array();
           
            include 'PHPClasses/Student.php';
             
            $Student=new Student();
            
            $getStudentQuery=mysql_query("SELECT * FROM student") 
            or die('Could not connect: ' . mysql_error());
            
            
            echo "<table id=hor-minimalist-a>";
            echo "<thead><tr><th scope=col>Student Name</th>
            <th scope=col>Status</th>
            <th scope=col>Promoted to</th>
            <th scope=col>Archive</th>
           
            </thead>";
           
            $counter=0;
            while($row=mysql_fetch_array($getStudentQuery)){
               extract($row);
               
               //check if student passed
               $passed=$this->checkifPassed($idStudent);
           
               
                if($passed){
                       //archive the grades
                       $archiveGrades=mysql_query("UPDATE grade SET archived='1'WHERE student_idStudent=$idStudent")
                            or die('Could not connect: ' . mysql_error());
                    
                        $key=array_search($studentlvl,$arrayLevels);
                        $key++;
                        
                        $Student->setStudentName($studentName);
                        $Student->setAddress($address);
                        $Student->setStudentLevel($arrayLevels[$key]);
                        $Student->setGender($gender);
                        $Student->setAge($key);
                        $Student->setIdStudent($idStudent);

                        //promote student
                        $this->updateDB($Student);
                        
                       
                        //update student 
                        $updateStudentPassed=mysql_query("UPDATE student SET passed='0'  WHERE idStudent=".$Student->getIdStudent()) 
                            or die('Could not connect: ' . mysql_error());
                         
                        echo "<tr><td><p>$studentName</p></td>";
                        echo "<td><p>PASSED</p></td><td><p>".$arrayLevels[$key]."</p></td><td><p>SUCCESSFUL</p></td></tr>";
                }
                
               
                
            
               else{
                     //add student to array of failed students
                      $failed[$counter]=$studentName;
                      $counter++;
                      
               }
            
              
                     
                
         
            }
             echo "</table>\n<br/><br/><br/><br/>"; 
            
            //Show students that failed
            echo "<p>The following students failed to meet the standards of his or her subject. Retakes are needed:</p>";      
            echo "<table id=hor-minimalist-a>";
            echo "<thead><th scope=col>Student Name</th><th scope=col>Status</th><th scope=col>Archive</th>
            </thead>";
            
            for($counter=0;$counter<count($failed);$counter++){
                echo "<td><p>". $failed[$counter]."</p></td>";
                echo "<td><p>FAILED</p></td></td><td><p>FAILED</p></td></tr>";
            }
               echo "</table>\n<br/><br/><br/><br/>"; 
            
           
        }
        
        //returns passed
        private function checkifPassed($studentID){
            
            $getPassedQuery= mysql_query("SELECT passed FROM student WHERE idStudent=".$studentID);
            $row = mysql_fetch_array($getPassedQuery);
            extract($row);
            
            return $passed;
            
        }
        
        
        public function viewGradeHistory($idStudent){
            
            //View grades in Grade 1
            echo "<p>Grade 1 </p>";
            
            $this->viewStudentGrades($idStudent, "Grade 1", 1);
            
            //View grades in Grade 2            
            echo "<p>Grade 2</p>";
            
            $this->viewStudentGrades($idStudent, "Grade 2", 1);
            
           //View grades in Grade 3
            echo "<p>Grade 3 </p>";
            $this->viewStudentGrades($idStudent, "Grade 3", 1);
 
           //View grades in Grade 4           
            echo "<p>Grade 4 </p>";
            
            $this->viewStudentGrades($idStudent, "Grade 4", 1);
            
           //View grades in Grade 5         
            echo "<p>Grade 5</p>";
            
            $this->viewStudentGrades($idStudent, "Grade 5", 1);
           
           //View grades in Grade 6
            echo "<p>Grade 6 </p>";
            
            $this->viewStudentGrades($idStudent, "Grade 6", 1);
           
           //View grades in 1st Year 
            echo "<p>1st Year</p>";
            
            $this->viewStudentGrades($idStudent, "1st Year", 1);
          
           //View grades in 2nd Year  
            echo "<p>2nd Year</p>";
            
            $this->viewStudentGrades($idStudent, "2nd Year", 1);
           
           //View grades in 3rd Year 
            echo "<p>3rd Year </p>";
            $this->viewStudentGrades($idStudent, "3rd Year", 1);
          
           //View grades in 4th Year
            echo "<p>4th year</p>";
             $this->viewStudentGrades($idStudent, "4th Year", 1);
        } 
        
        
      public function checkLevelifArchived($idStudent){
          
            $levelsArchived=mysql_query("SELECT level FROM grade WHERE archived=1 and student_idStudent=".$idStudent." GROUP BY level");
      
            return $levelsArchived;
      }
        
}




    
?>
